<template>
  <el-dialog
    title="设置标签"
    :visible="visible"
    width="750px"
    center
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="dataList"
      :rules="rules"
      label-width="82px">

      <el-form-item label="标签:" prop="tag">
        <ele-icon-picker
          v-model="form.icon"
          @setTagList=setTagList
          placeholder="请选择菜单图标"/>
      </el-form-item>
      <el-form-item label="" prop="">
        <div v-if="tagList.length>0">
          <el-tag  v-for="(item,index) in tagList" :key="index" :type=item.tag_color closable  @close="handleClose(index)" >{{item.tag_name}}</el-tag>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import EleIconPicker from '../index';
export default {
  name: "add-tag",
  components: {EleIconPicker},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);


        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {

      },
      dataList:{
        edm_template:'',
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      edmTemplate:{},
      tagList:[],
      tagIds:[],
    };
  },
  mounted() {
    this.getEdmTemplate();
  },
  methods:{

    setTagInfo(row){
      var tagIds = [];
      this.tagList = row.tag_list;
      row.tag_list.forEach(item => {
        tagIds.push(item.id);
      });
      this.tagIds = tagIds;
    },
    getEdmTemplate(){
      this.$http.get('/activate/getEdmTemplate', this.form).then((res) => {
        this.loading = false;
        if (res.data.code === 0) {
          this.edmTemplate = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    setTagList(val)
    {
      this.tagList.push({
        'id':val.id,
        'tag_name':val.tag_name,
        'tag_color':val.tag_color
      });
      var tagIds = [];
      this.tagList.forEach(item => {
        tagIds.push(item.id);
      });
      this.tagIds = tagIds;
    },
    /* 删除标签id */
    handleClose(index)
    {
      this.tagList.splice(this.tagList.indexOf(index), 1);
      var tagIds = [];
      this.tagList.forEach(item => {
        tagIds.push(item.id);
      });
      this.tagIds = tagIds;
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (this.tagIds.length==0) valid =false;
        if (valid) {
          this.loading = true;
          this.$http.post('/activate/addTag',{id:this.form.id,tag_ids:this.tagIds}).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          this.$message.error('请至少选择一个标签');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
